<?php

class Emailmodel extends CI_Model {

	function Emailmodel() {
		// Call the Model constructor
		parent::__construct();
		//$this->load->plugin('phpmailer/phpmailer');
		include 'phpmailer/phpmailer.php';
	}

	function sendmail_wth_attachment($to, $from, $bbc, $subject, $message, $text, $attachment) {

		$mail = new PHPMailer();

		$mail->IsSMTP();
		$mail->SMTPAuth = true;                  // enable SMTP authentication
		$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
		$mail->Host = "smtp.gmail.com";      // sets GMAIL as the SMTP server
		$mail->Port = 465;                   // set the SMTP port

		$mail->Username = "residential@jllresidential.com";  // GMAIL username
		$mail->Password = "jllresidential2011";            // GMAIL password


		$mail->From = $from;
		$mail->FromName = $from;
		$mail->Subject = $subject;
		$mail->Body = $message;                      //HTML Body
		$mail->AltBody = $text; //Text Body
		//print_r($mail);
		$mail->WordWrap = 50; // set word wrap

		$addresses = explode(',', $to);

		foreach ($addresses as $address) {
			$mail->AddAddress($address);
		}

		$mail->AddReplyTo($from, $from);

		$mail->IsHTML(false); // send as HTML
		$mail->AddAttachment($attachment['source'], $attachment['filename']);

		if (!$mail->Send()) {
			$err = "Mailer Error: " . $mail->ErrorInfo;
			return $err;
		} else {
			return true;
		}
	}

	function sendmail_wth_attachments($to, $from, $bbc, $subject, $message, $text, $attachments) {

		$mail = new PHPMailer();

		$mail->IsSMTP();
		$mail->SMTPAuth = true;                  // enable SMTP authentication
		$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
		$mail->Host = "smtp.gmail.com";      // sets GMAIL as the SMTP server
		$mail->Port = 465;                   // set the SMTP port

		$mail->Username = "residential@jllresidential.com";  // GMAIL username
		$mail->Password = "jllresidential2011";            // GMAIL password

		$mail->From = $from;
		$mail->FromName = $from;
		$mail->Subject = $subject;
		$mail->Body = $message;                      //HTML Body
		$mail->AltBody = $text; //Text Body
		//print_r($mail);
		$mail->WordWrap = 50; // set word wrap

		$addresses = explode(',', $to);

		foreach ($addresses as $address) {
			$mail->AddAddress($address);
		}

		$mail->AddReplyTo($from, $from);

		$mail->IsHTML(false); // send as HTML

		if (count($attachments) > 0) {
			foreach ($attachments as $attachment) {
				$mail->AddAttachment($attachment['source'], $attachment['filename']);
			}
		}



		if (!$mail->Send()) {
			$err = "Mailer Error: " . $mail->ErrorInfo;
			return $err;
		} else {
			return true;
		}
	}

	function sendmail($to, $from, $bbc, $subject, $message, $text) {

		$mail = new PHPMailer();

		$mail->IsSMTP();
		$mail->SMTPAuth = true;                  // enable SMTP authentication
		$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
		$mail->Host = "smtp.gmail.com";      // sets GMAIL as the SMTP server
		$mail->Port = 465;                   // set the SMTP port

		$mail->Username = "residential@jllresidential.com";  // GMAIL username
		$mail->Password = "jllresidential2011";            // GMAIL password


		$mail->From = $from;
		$mail->FromName = $from;
		$mail->Subject = $subject;
		$mail->Body = $message;                      //HTML Body
		$mail->AltBody = $text; //Text Body
		//print_r($mail);
		$mail->WordWrap = 50; // set word wrap

		$addresses = explode(',', $to);

		foreach ($addresses as $address) {
			$mail->AddAddress($address);
		}

		$mail->AddReplyTo($from, $from);

		$mail->IsHTML(false); // send as HTML

		if (!$mail->Send()) {
			$err = "Mailer Error: " . $mail->ErrorInfo;
			return $err;
		} else {
			return true;
		}
	}

	//get admin email
	function get_admin_email($formid) {
		$query = $this->db->query("SELECT * FROM Forms WHERE Form_ID =" . $this->db->escape($formid));

		if ($query->num_rows() > 0) {
			return $query->row();
		} else {
			return false;
		}
	}

	//update admin email
	function update_admin_email($formid, $new_email) {
		$query = $this->db->query("UPDATE Forms SET AdminAddress = " . $this->db->escape($new_email) . " WHERE idform =" . $this->db->escape($formid));
	}

}

?>